/*
 * ECU_TJA1145.c
 *
 *  Created on: 2018年11月29日
 *      Author: 40786
 */

#include "main.h"



lpspi_tx_cmd_config_t SBC_Lpspi2_TxCfg =
{
	.whichPcs = LPSPI_PCS0,							/* PCS=1: Transfer using PCS1 */
	.width = LPSPI_SINGLE_BIT_XFER,					/* WIDTH=0: Single bit transfer */
	.clkPhase = LPSPI_CLOCK_PHASE_2ND_EDGE,			/* CPHA=0: Change data on SCK lead'g, capture on trail'g edge*/
	.clkPolarity = LPSPI_SCK_ACTIVE_HIGH,			/* CPOL=0: SCK inactive state is HIGH */
	.lsbFirst = false,								/* LSBF=0: Data is transfered MSB first */
	.txMask 	= false,							/* TXMSK=0: Normal transfer: data loaded from tx FIFO */
	.rxMask		= false,							/* RXMSK=0: Normal transfer: rx data stored in rx FIFO */
	.contTransfer = true,							/* CONTC=0: Continuous transfer disabled */
	.contCmd = false ,								/* Master option to change cmd word within cont transfer. */
	.frameSize = 16,								/* FRAMESZ=7: # bits in frame = 7+1=8 */
	.preDiv    = 2,									/* PRESCALE=2: Functional clock divided by 2*2 = 4 */
	.byteSwap = false, 								/* BYSW=0: Byte swap disabled */
};



Bsw_Lpspi_Cfg 	SBC_LPSPI2_Cfg =
{
	.base = LPSPI2,									/*SPI硬件模块选择:  LPSPI0*/
	.clockName = LPSPI2_CLK,
	.clockSource = 6,
	.b_TxDmaCmd = false ,							/* TXDMA not used */
	.b_RxDmaCmd = false ,							/* RXDMA not used */
	.Lpspi_mode = LPSPI_MASTER,						/* Set for master mode */

	.PinCfg = LPSPI_SDI_IN_SDO_OUT,
	.DataOutCfg = LPSPI_DATA_OUT_RETAINED,
	.b_Pcs_Enable = true,
	.b_SampleDly = 	false,
	.txWater = 1,
	.rxWater = 0,
	.Cmd_Cfg = &SBC_Lpspi2_TxCfg,
	.u32_SCKDIV = 15,
	.u32_SCKPCS = 20,
	.u32_PCSSCK = 20,
	.u32_DBT = 20,
	.Lpspi_Pcs = LPSPI_PCS0,
  .PcsPolarity = LPSPI_ACTIVE_LOW,
};

void ECU_1145_PORT_INIT(void)
{
	BSW_GPIO_MUX_CONFIG(Bsw_Port_C,15,PORT_MUX_ALT3);			/* 	PC15: 	MUX = ALT3, LPSPI0_SCK 	*/
	BSW_GPIO_MUX_CONFIG(Bsw_Port_C,14,PORT_MUX_ALT3);			/* 	PD14: 	MUX = ALT3, LPSPI0_PCS 	*/
	BSW_GPIO_MUX_CONFIG(Bsw_Port_C,1,PORT_MUX_ALT3);			/* 	PC1: 	MUX = ALT3, LPSPI0_SOUT */
	BSW_GPIO_MUX_CONFIG(Bsw_Port_C,0,PORT_MUX_ALT3);			/* 	PC0: 	MUX = ALT3, LPSPI0_SIN */

	BSW_LPSPI_INIT(SBC_LPSPI2_Cfg);

	/*
	BSW_DMAMUX_INIT(SPI0_TxDma.DMA_Ch,EDMA_REQ_LPSPI0_TX);
	SPI0_TxDma.SrcAddr = (u32)TCD0_Source;
	SPI0_TxDma.DestAddr = (u32)&(LPSPI0->TDR);
	BSW_DMA_INIT_CFG(SPI0_TxDma);

	BSW_DMAMUX_INIT(SPI0_RxDma.DMA_Ch,EDMA_REQ_LPSPI0_RX);
	SPI0_RxDma.SrcAddr = (u32)&(LPSPI0->RDR);
	SPI0_RxDma.DestAddr = (u32)TCD0_Dest;
	BSW_DMA_INIT_CFG(SPI0_RxDma);
	*/
}

u16 TJA1145_SPI2_Write(uint8 Addr,uint8 data)
{
	u16 rxdata,Txdata;
	Txdata=(Addr<<9)+data;
	BSW_LPSPI_START(LPSPI2,Txdata);
	rxdata=BSW_LPSPI_END(LPSPI2);
	return rxdata;
}
u16 TJA1145_SPI2_Read(uint8 Addr)
{
	u16 rxdata,Txdata;
	Txdata=(Addr<<9)+0x100;
	BSW_LPSPI_START(LPSPI2,Txdata);
	rxdata=BSW_LPSPI_END(LPSPI2);
	return rxdata;
}


void TJA1145_Init_mode(void)
{
	TJA1145_SPI2_Write(Mode_Control,0x07);// MC:  normal mode
	TJA1145_SPI2_Write(CAN_Control,0x02);// 关闭CAN_FD , 关闭CAN选择唤醒 ,   CAN Active mode
	TJA1145_SPI2_Write(Data_Rate,0x05);//  波特率500K
	TJA1145_SPI2_Read(Identification); //

}


void TJA1145_GO_Sleep(void)
{
	gUds_CanTxAppFlag=CAN_STOP;
	gUds_CanRxAppFlag=CAN_STOP;
	TJA1145_SPI2_Read(Event_Capture_Status); //清除中断事件
	TJA1145_SPI2_Read(CAN_Control); //清除中断事件
	TJA1145_SPI2_Read(Transceiver_Event_Enable); //清除中断事件
	TJA1145_SPI2_Write(System_Event_Status,0x10);// 清除PO
	TJA1145_SPI2_Write(Transceiver_Event_Enable,0x01);// CAN wake_up enable
	TJA1145_SPI2_Write(Mode_Control,0x01);//


}

